package com.luyu.exception;


import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luyu.res.ResultRes;
import com.luyu.util.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class PemissionDeniedHandler implements AccessDeniedHandler {
    @Resource
    private ObjectMapper mapper;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.info("未授权/无权限");
        accessDeniedException.printStackTrace();
        ResponseUtil.returnErrorMsg(response,ResultRes.error(accessDeniedException.getMessage(), HttpStatus.HTTP_UNAUTHORIZED));
    }
}
